import os
import sys
import re
msyn_fold=sys.argv[1]
pos_dict={}
syn_dict={}
for msyn in os.listdir(msyn_fold):
    if msyn.startswith('mSynF'):
        pos_file=open(msyn_fold+'/'+msyn+'/species.gff_pos','r')
        for line in pos_file:
            line_list=line.strip().split('\t')
            pos_dict[line_list[0]]=[line_list[3],int(line_list[4].split(':')[0]),int(line_list[-1].split(':')[1])]
        pos_file.close()
        for syn in os.listdir(msyn_fold+'/'+msyn):
            if syn.endswith('syn.seg'):
                num1=re.findall('\d+',syn)[0]
                num2=re.findall('\d+',syn)[1]
                if num1 not in syn_dict.keys():
                    syn_dict[num1]=[]
                if num2 not in syn_dict.keys():
                    syn_dict[num2]=[]
                syn_file=open(msyn_fold+'/'+msyn+'/'+syn,'r')
                for line in syn_file:
                    if line.startswith('#'):
                        continue
                    line_list=line.strip().split('\t')
                    syn_dict[num1].append(line_list[0])
                    syn_dict[num2].append(line_list[1])
                
      
species_name_list=['Tomato','Potato','viarum','lyratum','melongena','Pepper','Golden','Wolfberry','Datura','biflora']
index=0
for i in species_name_list:
    trans_file=open('../../'+i+'.pav.txt','r')
    out_file=open(i+'.trans_bed','w')
    out_file2=open(i+'.syn_bed','w')
    for line in trans_file:
        line_list=line.strip().split('\t')
        if line_list[2]!='no':
            for x in pos_dict[line_list[2]]:
                out_file.write(str(x)+'\t')
            out_file.write('\n')
    
    index+=1
    for gene in syn_dict[str(index)]:
        for x in pos_dict[gene]:
            out_file2.write(str(x)+'\t')
        out_file2.write('\n')
           
trans_file
            